package mirkofrancuski.battleship.game;

import java.util.HashMap;
import java.util.Random;

import mirkofrancuski.battleship.db.BoardTable;
import mirkofrancuski.battleship.db.CoordinateTable;
import mirkofrancuski.battleship.db.IntTable;

import android.database.sqlite.SQLiteDatabase;

public class ComputerEasy implements Player {

	protected Random randomGenerator;

	protected Board myBoard, enemyBoard;

	protected Coordinates lastShot = null;

	protected int noOfShots = 0;

	public ComputerEasy() {
		super();
		randomGenerator = new Random();
		myBoard = new Board();
		enemyBoard = new Board();

		placement();
	}

	public void placement() {
		myBoard.randomPlacement();

		/* TEST */

		// Coordinates coord = new Coordinates();
		// for(Cell shipCell : Board.ships){
		//
		// myBoard.placeShip(new Ship(shipCell, new Coordinates(coord),
		// Direction.HORIZONTAL));
		// coord.horizontal ++;
		// }
	}

	@Override
	public Coordinates getShotCoordinates() {
		lastShot = new Coordinates();

		while (true) {
			lastShot.horizontal = randomGenerator.nextInt(Board.BOARD_SIZE);
			lastShot.vertical = randomGenerator.nextInt(Board.BOARD_SIZE);
			if (enemyBoard.isEmpty(lastShot))
				break;

		}
		noOfShots++;
		return lastShot;
	}

	@Override
	public void resultFromLastShot(Cell cell) {
		enemyBoard.updateResult(lastShot, cell);
	}

	@Override
	public void resultFromLastShot(Ship ship) {
		enemyBoard.updateResult(ship);
	}

	@Override
	public int getNoOfShots() {
		return noOfShots;
	}

	@Override
	public Cell takeHit(Coordinates coord) {
		Cell retCell = myBoard.takeHit(coord);
		return retCell;
	}

	@Override
	public Ship getShip(Cell ship) {
		return myBoard.selectPlacedShip(ship);
	}

	public boolean areAllShipsSunk() {
		return myBoard.areAllShipsSunk() && noOfShots >= 17;
	}

	public void randomPlacement() {
		myBoard.randomPlacement();
	}

	public void printBoard() {
		System.out.println(myBoard.toString());
	}

	public void printEnemyBoard() {
		System.out.println(enemyBoard.toString());
	}
	
	
	public void save(SQLiteDatabase db){
		String className = ComputerEasy.class.toString();
		
		BoardTable.saveBoard(db, enemyBoard, BoardTable.BOARD_GAME_COMPUTER_MAIN);
		BoardTable.saveBoard(db, myBoard, BoardTable.BOARD_GAME_COMPUTER_SECUNDARY);
		
		HashMap<String, Coordinates> coordinatesHashMap = new HashMap<String, Coordinates>();
		coordinatesHashMap.put("lastShot", lastShot);		
		CoordinateTable.addCoordinates(db, className, coordinatesHashMap);
		
		IntTable.addInt(db, className, "noOfShots", noOfShots);
	}
	
	public void restore(SQLiteDatabase db){
		String className = ComputerEasy.class.toString();
		
		enemyBoard = BoardTable.restoreBoard(db, BoardTable.BOARD_GAME_COMPUTER_MAIN);
		myBoard = BoardTable.restoreBoard(db, BoardTable.BOARD_GAME_COMPUTER_SECUNDARY);
		
		
		HashMap<String, Coordinates> coordinatesHashMap = CoordinateTable.getCoordinates(db, className);
		lastShot = coordinatesHashMap.get("lastShot");
		
		HashMap<String, Integer> hashMap = IntTable.getInts(db, className);
		noOfShots = hashMap.get("noOfShots");
	
	}
	
	

}
